<template>
  <el-dialog
    title="选择企业人员"
    :visible.sync="visible"
    width="700px"
    :before-close="cancel"
    append-to-body
  >
    <el-row :gutter="15">
      <el-col :span="12">
        <div class="transfer-box">
          <div class="transfer-title">{{ checkedIds.length }}/{{ total }} 条数据</div>
          <div class="transfer-search">
            <el-input
              v-model="searchValue"
              placeholder="请输入内容"
              prefix-icon="el-icon-search"
              clearable
              @input="onSearch"
            ></el-input>
          </div>
          <ul v-infinite-scroll="infiniteLoad" v-loading="loading" class="teansfer-box-ul">
            <li v-for="item in list" :key="item.id" @click="onItemClick(item, $event)">
              <el-checkbox :value="item.checked" @change="onCheckChange(item)"></el-checkbox>
              <span class="text-1">{{ item.realName }}</span>
              <span class="text-2">{{ item.mobilePhone }}</span>
              <span class="text-3">{{ item.deptName }}</span>
            </li>
          </ul>
          <p v-if="isInfiniteLoading" class="infinite-loading">加载中...</p>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="transfer-box">
          <div class="transfer-title">{{ checkedIds.length }} 条数据</div>
          <div class="transfer-search">
            <el-input
              v-model="searchCheckedValue"
              placeholder="请输入内容"
              prefix-icon="el-icon-search"
              clearable
            ></el-input>
          </div>
          <ul class="teansfer-box-ul">
            <li v-for="item in showCheckedList" :key="item.id">
              <span class="text-1">{{ item.realName }}</span>
              <span class="text-2">{{ item.mobilePhone }}</span>
              <span class="text-3">{{ item.deptName }}</span>
              <i class="el-icon-error" @click="deleteChecked(item.id)"></i>
            </li>
          </ul>
        </div>
      </el-col>
    </el-row>

    <div class="footer-btn">
      <span class="footer-tips">
        发送后，将会在“薪福利”中推送给已选人员，未选择的人员不会看到该活动{{ title }}。
      </span>
      <div>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="ok">发送薪友</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
  import { getAllUser } from '@/api/careCard';

  export default {
    props: {
      title: {
        type: String,
        required: true,
      },
    },
    data() {
      return {
        visible: false,
        organizationId: 0,
        searchValue: '',
        list: [],
        checkedList: [],
        current: 1,
        size: 20,
        isInfiniteLoading: false,
        isFirstLoad: true, // 数据是否是初始化加载
        isLastLoad: false, // 数据是否加载完毕
        loading: false,
        total: 0,
        searchCheckedValue: '',
      };
    },
    computed: {
      checkedIds() {
        return this.checkedList.map(item => item.id);
      },
      showCheckedList() {
        return this.searchCheckedValue === ''
          ? this.checkedList.map(item => ({ ...item }))
          : this.checkedList.reduce((col, item) => {
              [item.mobilePhone || '', item.realName || '', item.deptName || ''].some(val =>
                val.includes(this.searchCheckedValue)
              )
                ? col.push({ ...item })
                : null;
              return col;
            }, []);
      },
    },
    methods: {
      async initData(changeTotal = true) {
        this.current = 1;
        this.loading = true;
        this.isFirstLoad = true;
        this.isLastLoad = false;
        this.isInfiniteLoading = false;
        const params = {
          current: this.current,
          size: this.size,
          organizationId: this.organizationId,
          keyWordEx: this.searchValue,
          dataType: 1, // 过滤离职人员
        };
        this.list = [];
        const { data } = await getAllUser(params);
        this.loading = false;
        this.isFirstLoad = false;
        const ids = this.checkedIds;
        this.list = data.records.map(item => {
          item.checked = ids.includes(item.id);
          return item;
        }, []);
        if (changeTotal) {
          this.total = data.total;
        }
      },
      show(organizationId) {
        this.organizationId = organizationId;
        this.checkedList = [];
        this.searchValue = '';
        this.searchCheckedValue = '';
        this.visible = true;
        this.initData();
      },
      cancel() {
        this.visible = false;
        this.list = [];
        this.total = 0;
        this.checkedList = [];
        this.searchValue = '';
        this.searchCheckedValue = '';
        this.isFirstLoad = true;
        this.isLastLoad = false;
        this.isInfiniteLoading = false;
        this.current = 1;
      },
      ok() {
        this.$emit('on-ok', this.checkedList);
        this.cancel();
      },
      async onSearch() {
        this.initData(false);
      },
      onItemClick(item, event) {
        const target = event.target;
        if (target.tagName !== 'INPUT') {
          item.checked = !item.checked;
          this.onCheckChange(item);
        }
      },
      onCheckChange(item) {
        if (item.checked) {
          if (this.checkedIds.includes(item.id)) {
            this.checkedList.forEach(chkItem => {
              item.id === chkItem.id ? (chkItem.checked = item.checked) : null;
            });
          } else {
            this.checkedList.push({ ...item });
          }
        } else {
          this.checkedList = this.checkedList.filter(chkItem => chkItem.id !== item.id);
        }
      },
      deleteChecked(id) {
        this.checkedList = this.checkedList.filter(item => item.id !== id);
        this.list.forEach(item => {
          item.id === id ? (item.checked = false) : null;
        });
      },
      async infiniteLoad() {
        if (this.isFirstLoad || this.isLastLoad) return;
        this.isInfiniteLoading = true;
        this.current = this.current + 1;
        const params = {
          current: this.current,
          size: this.size,
          organizationId: this.organizationId,
          keyWordEx: this.searchValue,
        };
        const { data } = await getAllUser(params);
        if (data.records.length > 0) {
          this.list = [...this.list, ...data.records];
        } else {
          this.isLastLoad = true;
        }
        this.isInfiniteLoading = false;
      },
    },
  };
</script>
<style lang="scss" scoped>
  // 选择人员
  .transfer-box {
    width: 100%;
    height: 400px;
    border-radius: 14px;
    border: 1px solid #dddddd;
    position: relative;
    .transfer-title {
      padding: 15px 15px;
      border-bottom: 1px solid #dddddd;
    }
    .transfer-search {
      padding: 10px 15px;
    }
    .teansfer-box-ul {
      margin: 0;
      padding: 0;
      height: calc(100% - 105px);
      overflow-y: auto;
      > li {
        list-style: none;
        line-height: 32px;
        height: 32px;
        display: flex;
        padding: 0 15px;
        cursor: pointer;
        &:hover {
          background: #f5f7fa;
        }
        > span {
          display: inline-block;
          width: 95px;
          text-align: center;
          padding-left: 5px;
          text-overflow: hidden;
          overflow: hidden;
          &.text-1 {
            width: 80px;
          }
        }

        > i {
          font-size: 16px;
          color: #b8b8b8;
          padding: 8px 0 8px 5px;
        }
      }
    }
  }
  .infinite-loading {
    position: absolute;
    bottom: 0;
    text-align: center;
    padding: 5px;
    width: 313px;
    margin: 0;
    background: #ffffff;
    border-radius: 14px;
    z-index: 100;
  }
  .footer-btn {
    display: flex;
    justify-content: space-between;
    margin: 10px;
  }
  .footer-tips {
    font-size: 13px;
    color: #7f848b;
    line-height: 26px;
    width: 50%;
  }
  .el-dialog__body {
    padding: 0 !important;
  }
</style>
